package com.yunqiao.api.controller;

import java.util.Map;


import com.yunqiao.common.consts.AppConsts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.service.api.entity.ApplyDriverEntity;
import com.yunqiao.service.api.ApplyDriverService;
import com.yunqiao.common.utils.PageUtils;


/**
 * 用户申请为司机
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-16 15:25:51
 */
@RestController
@RequestMapping("api/applydriver")
@Api(tags = "ApplyDriverApiController" ,description = "用户申请为司机管理")
public class ApplyDriverApiController {

    @Autowired
    private ApplyDriverService applyDriverService;

    /**
     * 列表
     */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", required = false ,defaultValue = "1",paramType = "query" , dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false,defaultValue = "10",paramType = "query" , dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询用户申请为司机列表")
    public ApiResponse<PageUtils<ApplyDriverEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = applyDriverService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个用户申请为司机")
    public ApiResponse<ApplyDriverEntity> info(@PathVariable("id") Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        ApplyDriverEntity applyDriver = applyDriverService.getById(id);
        return new ApiResponse<>(applyDriver);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存用户申请为司机")
    public ApiResponse save(ApplyDriverEntity applyDriver){
        ValidatorUtils.validateEntity(applyDriver);
        applyDriver.setStatus(AppConsts.USER_APPLY_DRIVER_1);
        applyDriverService.save(applyDriver);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改用户申请为司机")
    public ApiResponse update(@PathVariable Long id , ApplyDriverEntity applyDriver){
        ApplyDriverEntity dbItem = applyDriverService.getById(id);
        if(dbItem==null){
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        applyDriverService.updateById(applyDriver);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除用户申请为司机")
    public ApiResponse delete(@PathVariable Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        applyDriverService.removeById(id);
        return new ApiResponse<>();
    }

}
